<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class FollowersController extends Controller
{
    //
    public function __construct()
    {   //登陆验证
        $this->middleware('auth');
    }

    public function follow(User $user)
    {   //添加policy权限认证，当前用户不能操作别人的信息

        $this->authorize('follow',$user);
        if(!Auth::user()->isFollowing($user->id)){
            Auth::user()->follow($user->id);
        }
        return redirect()->route('users.show',$user->id);
    }

    public function unfollow(User $user)
    {
        //添加policy权限认证，当前用户不能操作别人的信息
        $this->authorize('follow',$user);
        if(Auth::user()->isFollowing($user->id)){
            Auth::user()->unfollow($user->id);
        }
        return redirect()->route('users.show',$user->id);
    }
}
